#include "ObjectManager.h"
#include "Renderer.h"
#include "IRenderable.h"


Renderer *ObjectManager::pRenderer = Renderer::getInstance();


ObjectManager::ObjectManager(void)
{
}


ObjectManager::~ObjectManager(void)
{
	Clear();
}

ObjectManager* ObjectManager::getInstance()
{
	static ObjectManager thisInstance;

	return &thisInstance;
}

void ObjectManager::Render()
{
	std::vector<IRenderable*>::iterator iter = m_Objects.begin();

	for(;iter != m_Objects.end();++iter)
		pRenderer->AddRenderable((*iter),(*iter)->getPriority());
}

void ObjectManager::Update()
{
	std::vector<IRenderable*>::iterator iter = m_Objects.begin();

	for(;iter != m_Objects.end();++iter)
		(*iter)->Update();
}

void ObjectManager::Clear()
{
	for(unsigned i = 0;i < m_Objects.size();++i)
		delete m_Objects[i];

	m_Objects.clear();
}

void ObjectManager::RemoveObject( const IRenderable *pObject )
{
	std::vector<IRenderable*>::iterator iter = m_Objects.begin();

	for(;iter != m_Objects.end();++iter)
		if((*iter) == pObject)
		{
			delete (*iter);
			m_Objects.erase(iter);
			break;
		}
}
